// +----------------------------------------------------------------------
// | CmsWing [ 美业管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 psi All rights reserved.
// +----------------------------------------------------------------------
// | Author: huang <2374266244@qq.com>
// +----------------------------------------------------------------------
'use strict';
import Base from './base.js';

export default class extends Base {
  /**
   * index action
   * @return {Promise} []
   */
	async userlistAction(){
		let page = this.post();
		let pages = page.pages;
		let limits = page.limits;
		let search = page.search;
		let zt = page.zt;
		let orid = this.userinfo.departmentid;
		let orcode = await this.model('or_department').where({id: orid}).getField('orcode', true)
		let dList = await this.model('or_department').where({orcode: orcode}).getField('id')
		console.log(dList)

		let data = await this.model('or_app_user')
				.alias('u')
				.field('u.*,p.name as pname,z.name as zname, d.name as shopname, ds.name as bumenname')
				.join("think_or_position p ON u.positionid = p.id")
				.join("think_or_position z ON u.zhichengid = z.id")
				.join('think_or_department d on u.shopid = d.id')
				.join('think_or_department ds on u.departmentid = ds.id')
				.where({'u.departmentid':['in', dList], 'u.isdisable':zt, 'u.name|u.username':['like','%'+search+'%']})
				.page(pages, limits)
				.order('u.id DESC')
				.countSelect();

		return this.json(data);
	}

	// 添加、编辑员工
	async addeditAction(){
		let data = this.post();
		let orid = this.userinfo.departmentid;
		let addtime = Math.round(new Date().valueOf()/1000);

		let bumenInfo = await this.model('or_department').where({id: data.departmentid}).find()

		switch(bumenInfo.floor){
			case 1:
				data.bumenid = 0
				data.xiaozuid = 0
				data.shopid = bumenInfo.id
				data.departmentid = bumenInfo.id
				break
			case 2:
				data.bumenid = bumenInfo.id
				data.xiaozuid = 0
				data.shopid = bumenInfo.pid
				data.departmentid = bumenInfo.id
				break
			case 3:
				data.bumenid = bumenInfo.pid
				data.xiaozuid = bumenInfo.id
				data.shopid = await this.model('or_department').where({id: bumenInfo.pid}).getField('pid', true)
				data.departmentid = bumenInfo.id
				break 
		}

		if (data.id*1) {
			let chongfu = await this.model('or_app_user').where({username:data.username, departmentid:orid, id:['!=',data.id]}).select();
			if(!think.isEmpty(chongfu)){
				return this.fail('该手机号码已经存在！');
			}
			data.password = think.md5(data.username)
			console.log(data.ismanage);
			let gengxin = await this.model('or_app_user').update(data);
			return gengxin?this.success('编辑成功'):this.fail('编辑失败');
		}

		let repeat = await this.model('or_app_user').where({username:data.username, departmentid:orid}).select();
		if(!think.isEmpty(repeat)){
			return this.fail('该手机号码已经存在！');
		}

		data.password = think.md5(data.username);
		data.addtime = addtime;
		let res = await this.model('or_app_user').add(data);
		return res?this.success("添加成功！"):this.fail('添加失败！');
	}

	async unstopAction(){
		let id = this.post('id');
		let st = this.post('st');
		let res = await this.model('or_app_user').where({id:id}).update({isdisable: st});
		if (st*1) return res?this.success("停用成功"):this.fail('停用失败');
		else return res?this.success("复职成功"):this.fail('复职失败');
	}

		// 重置当前员工帐号和密码
	async resetpasswordAction(){
		let uid = this.get('id');

		let uinfo = await this.model('or_app_user').where({id: uid}).find();

		let password = think.md5(uinfo.username);

		let resetRes = await this.model('or_app_user').where({id: uid}).update({password: password});

		return resetRes?this.success('重置员工密码成功'):this.fail('重置员工密码失败');
	}

	async getDepartmentListAction () {
		let orcode = await this.model('or_department').where({id: this.userinfo.departmentid}).getField('orcode', true)
		let list = await this.model('or_department').where({orcode: orcode}).select()
		return this.json(list)
	}

}